package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.thread.WmThreadLocal;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {
    @Autowired
    private FileStorageService fileStorageService;

    /**
     * 图片上传
     *
     * @param file
     * @return
     */
    @Override
    public ResponseResult upload(MultipartFile file) {
        try {
            //1.1 获取图片名称
            String originalFilename = file.getOriginalFilename();//获取原始名称，aa.jpg
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));//获取后缀名
            String fileName = UUID.randomUUID().toString()+suffix;


            //1. 上传图片到minio中
            String path = fileStorageService.uploadImgFile("", fileName, file.getInputStream());

            //2. 新增wm_material表
            WmMaterial wmMaterial = new WmMaterial();
            wmMaterial.setUrl(path);
            wmMaterial.setCreatedTime(new Date());
            wmMaterial.setType((short) 0);
            wmMaterial.setIsCollection((short) 0);

            WmUser wmUser = WmThreadLocal.getWmUser();
            wmMaterial.setUserId(wmUser.getId());
            save(wmMaterial);
            return ResponseResult.okResult("上传图片成功");

        } catch (IOException e) {
            e.printStackTrace();
        }


        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"出现了异常情况");
    }

    /**
     * 图片列表查询
     * 单表查询用mybatisplus分页查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(WmMaterialDto dto) {
        //1. 参数校验
        if (dto.getPage() == null){
            dto.setPage(1);
        }
        if (dto.getSize() == null){
            dto.setSize(10);
        }

        //2. 执行分页查询
        IPage<WmMaterial> ipage = new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<WmMaterial> queryWrapper = Wrappers.lambdaQuery();
        //是否收藏
        if (dto.getIsCollection() == 1){//收藏
            queryWrapper.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        }

        //根据用户id查询
        queryWrapper.eq(WmMaterial::getUserId,WmThreadLocal.getWmUser().getId());

        ipage = page(ipage, queryWrapper);

        /**
         * 3. 封装数据
         * 参数1 表示当前页码
         * 参数2 表示每页现实的条数
         * 参数3 表示总条数
         */
        ResponseResult result = new PageResponseResult(dto.getPage(),dto.getSize(), (int) ipage.getTotal());
        result.setData(ipage.getRecords());

        return result;
    }

    /**
     * 收藏、取消收藏
     * @param id
     * @return
     */
    @Override
    public ResponseResult isCollection(Integer id) {
        //参数校验
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //获取前端id
        WmMaterial wmMaterial = getById(id);
        //进行判断
        if (wmMaterial.getIsCollection() == 1){
            wmMaterial.setIsCollection((short) 0);
        }else {
            wmMaterial.setIsCollection((short) 1);
        }
        //修改
        updateById(wmMaterial);
        return ResponseResult.okResult(wmMaterial);
    }

    /**
     * 删除素材
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deletePicture(Integer id) {
        //校验参数
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmMaterial wmMaterial = getById(id);
        return ResponseResult.okResult(removeById(wmMaterial));
    }
}
